<?php

/**
 * CityBao (C)2010-2099 CityBao Inc.
 * This is NOT a freeware, use is subject to license terms
 *
 * Id: member_user.php 2011-1-4 11:10:01
 */
if (!defined('CITY_BAO') || !defined('CB_ADMIN')) {
    exit('Access Denied');
}
if (!in_array($action, array('add', 'list', 'delete', 'verify'))) {
    $action = 'list';
}
if ('list' === $action) {
    require_once CB_INCLUDE . 'datalist.class.php';
    $page = $request->getParam('page');
    $sql = 'SELECT * FROM {#cbdbPrefix}members where 1';
    $datalist = new datalist ();
    $datalist->setQuery($sql);
    $datalist->setCurrentPage($page);
    $group_sql = "SELECT * FROM {#cbdbPrefix}usergroups where 1 order by listorder";
    $group_list_db = $cbdb->get_results($group_sql, ARRAY_A);
    $group_list = array();
    foreach ($group_list_db as $group){
        $group_list[$group['groupid']] = $group['groupname'];
    }
    $tpl->assign('group_list', $group_list);
    $tpl->assign('user_list', $datalist->getData());
    $tpl->assign('pageControl', $datalist->getPages());
    $tpl->display('member/user/list.tpl');
} elseif ('add' === $action) {
    $form_validate = form_validate('profile_edit');
    if ($request->isPost()) {
        $validate = trim($request->getParam('validate'));
        if ($validate == $form_validate) {
            $user_param = $request->getParam('user');
            $flag = true;
            if ($flag) {
                $temp_user = $cbdb->get_row('SELECT * FROM {#cbdbPrefix}members WHERE username="' . $user_param['username'] . '" LIMIT 0,1');
                if ($temp_user) {
                    $error = "用户名{$user_param['username']}已经存在！";
                    $flag = false;
                }
                unset($temp_user);
            }
            if ($flag) {
                $temp_user = $cbdb->get_row('SELECT * FROM {#cbdbPrefix}members WHERE email="' . $user_param['email'] . '" LIMIT 0,1');
                if ($temp_user) {
                    $error = "Email地址{$user_param['email']}已经被注册！";
                    $flag = false;
                }
                unset($temp_user);
            }
            if ($flag) {
                $error = register_check($user_param);
                if ($error === true) {
                    $insert_data = array(
                        'username' => $user_param['username'],
                        'password' => md5($user_param['password']),
                        'payword' => '',
                        'email' => $user_param['email'],
                        'groupid' => 3, //3个人会员
                        'newmessage' => '0',
                        'credit' => '0',
                        'regip' => '',
                        'regtime' => '',
                        'keep_loginip' => '',
                        'last_loginip' => '',
                        'last_logintime' => '',
                        'logintimes' => '0'
                    );
                    $result = $cbdb->insert('{#cbdbPrefix}members', $insert_data);
                    if ($result) {
                        $user_param['userid'] = $cbdb->get_last_insert_id();
                        $profile = $user_param;
                        unset($profile['username'], $profile['password'], $profile['password2'], $profile['email']);
                        $cbdb->insert('{#cbdbPrefix}member_profile', $profile);
                        $stats = array(
                            'userid' => $user_param['userid']
                        );
                        $cbdb->insert('{#cbdbPrefix}member_stats', $stats);
                        if (isset($config_member['log_user_register']) && $config_member['log_user_register']) {
                            $logger->log("{$user->username}于" . date('Y-m-d H:i:s', TIME_STAMP) . "在" . IP_ADDRESS . "处添加了新用户，用户名：{$user_param['username']}。", 'user_register', $user->username);
                        }
                    } else {
                        $error = '用户写入数据库失败，如果这个错误是第一次出现，请重试！如果多次出现，请与程序开发商(citybao.net)联系。';
                        $flag = false;
                    }
                } else {
                    $flag = false;
                }
            }
            if ($flag) {
                show_message('用户添加成功！', '?module=member_user&action=list');
            } else {
                show_message('用户添加失败！错误原因：' . $error);
            }
        } else {
            show_message('数据来源校验失败！', '-1');
        }
    } else {
        $tpl->assign('validate', $form_validate);
        $tpl->assign('idcardtype_options', include CB_DATA . 'options/idcardtype.php');
        $tpl->display('member/user/add.tpl');
    }
} elseif ('delete' === $action) {
    $userid = (array)$request->getParam('userid');
    print_r($userid);
} elseif ('verify' === $action) {

}